const Config = require('../../config').wechat
const Recording = require('../../database/models/Recording')
const WechatUser = require('../../database/models/WechatUser')

var Boom = require('boom'),
    // support for token signing and verification
    JasonWebToken = require('jsonwebtoken'),
    // the Hapi strategy for jwt
    JwtAuth = require('../../auth/jwt-strategy')

const Get = function (req, reply) {
  Recording.get(req.params._id, function (err, result) {
    if (err) {
      return reply(Boom.badImplementation(err));
    }
    reply(result);
  });
};

const GetList = function (req, reply) {
  WechatUser.getBySession(req.params.session3rd, function (err, user) {
    if (err) {
      return reply(Boom.badImplementation(err))
    }
    if (!user) {
      return reply(Boom.badRequest("user is null"))
    }
    if (!req.params.start) {
      req.params.start = new Date()
    }
    Recording.getList(user.openid, req.params.start, function (err, result) {
      if (err) {
        return reply(Boom.badImplementation(err))
      }
      console.log("getList:" + JSON.stringify(result))
      reply(result)
    })
  })
}

const Post = function (req, reply) {
  WechatUser.getBySession(req.payload.session3rd, function (err, user) {
    if (err) {
      return reply(Boom.badImplementation(err))
    }
    if (!user) {
      return reply(Boom.badRequest("user is null"))
    }

    var recording = new Recording({
      openid: user.openid,
      title: req.payload.title,
      recordTime: req.payload.recordTime,
      tempFilePath: req.payload.tempFilePath,
      update: req.payload.update,
      createTime: new Date()
    })

    recording.save(function(err, result) {
      if (err) {
        return reply(Boom.badImplementation(err))
      }
      reply(result)
    })
  })
}

const Put = function (req, reply) {
  WechatUser.getBySession(req.payload.session3rd, function (err, user) {
    if (err) {
      return reply(Boom.badImplementation(err))
    }
    if (!user) {
      return reply(Boom.badRequest("user is null"))
    }

    Recording.findById(req.payload._id, function (err, recording) {
      if (err) {
        return reply(Boom.badImplementation(err))
      }
      if (!recording) {
        return reply(Boom.badRequest("recording is null"))
      }

      recording.title = req.payload.title
      recording.recordTime = req.payload.recordTime
      recording.tempFilePath = req.payload.tempFilePath
      recording.upload = req.payload.upload
      recording.save(function(err, result) {
        if (err) {
          return reply(Boom.badImplementation(err))
        }
        reply(result)
      })
    })
  })
}

const Delete = function (req, reply) {
  WechatUser.getBySession(req.payload.session3rd, function (err, user) {
    if (err) {
      return reply(Boom.badImplementation(err))
    }
    if (!user) {
      return reply(Boom.badRequest("user is null"))
    }

    Recording.findByIdAndRemove(req.payload._id, function (err, recording) {
      if (err) {
        return reply(Boom.badImplementation(err))
      }
      reply(true)
    })
  })
}

module.exports = {
  Get,
  GetList,
  Post,
  Put,
  Delete
};
